package com.jp.juc.daemon;

import java.util.concurrent.TimeUnit;

/**
 * 用户线程不会因为 主线程结束而结束
 * <p>
 * 守护线程会随着JVM一同结束
 */
public class DaemonThread {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            boolean daemon = Thread.currentThread()
                    .isDaemon();
            System.out.println(Thread.currentThread().getName() + "---isDaemon---" + daemon);

            if (daemon) {
                System.out.println("守护线程");
            } else {
                System.out.println("用户线程");
            }
            while (true) {
            }
        }, "t1");
        // 设置为守护线程
        t1.setDaemon(true);
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName());

    }
}
